package com.xdcplus.web.service.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.CraftCheckOrderItemsMapper;
import com.xdcplus.web.common.pojo.entity.CraftCheckOrderItems;
import com.xdcplus.web.common.pojo.dto.CraftCheckOrderItemsDTO;
import com.xdcplus.web.common.pojo.dto.CraftCheckOrderItemsFilterDTO;
import com.xdcplus.web.common.pojo.vo.CraftCheckOrderItemsVO;
import com.xdcplus.web.common.pojo.query.CraftCheckOrderItemsQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.CraftCheckOrderItemsService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * 点检工单点检项明细表(CraftCheckOrderItems)表服务实现类
 *
 * @author Man.Chen
 * @since 2024-12-03 17:56:21
 */
@Slf4j
@Service("craftCheckOrderItemsService")
public class CraftCheckOrderItemsServiceImpl extends BaseServiceImpl<CraftCheckOrderItemsMapper, CraftCheckOrderItems, CraftCheckOrderItems, CraftCheckOrderItemsVO> implements CraftCheckOrderItemsService {

    @Autowired
    protected CraftCheckOrderItemsMapper craftCheckOrderItemsMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveCraftCheckOrderItems(CraftCheckOrderItemsDTO craftCheckOrderItemsDTO) {

        CraftCheckOrderItems craftCheckOrderItems = new CraftCheckOrderItems();
        BeanUtil.copyProperties(craftCheckOrderItemsDTO, craftCheckOrderItems);
        craftCheckOrderItems.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(craftCheckOrderItems);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateCraftCheckOrderItems(CraftCheckOrderItemsDTO craftCheckOrderItemsDTO) {

        CraftCheckOrderItems craftCheckOrderItems = this.getById(craftCheckOrderItemsDTO.getId());
        if (ObjectUtil.isNull(craftCheckOrderItems)) {
            log.error("updateCraftCheckOrderItems() The CraftCheckOrderItems does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(craftCheckOrderItemsDTO, craftCheckOrderItems);
        craftCheckOrderItems.setUpdatedUser(craftCheckOrderItemsDTO.getUpdatedUser());
        craftCheckOrderItems.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(craftCheckOrderItems);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<CraftCheckOrderItems> craftCheckOrderItemsList) {

        craftCheckOrderItemsList.forEach(craftCheckOrderItems -> {
            CraftCheckOrderItems craftCheckOrderItemsParam = new CraftCheckOrderItems();
            craftCheckOrderItemsParam.setId(craftCheckOrderItems.getId());
            if (ObjectUtil.isNotNull(craftCheckOrderItems.getId())) {
                craftCheckOrderItems.setId(craftCheckOrderItems.getId());
                craftCheckOrderItems.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<CraftCheckOrderItems> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(CraftCheckOrderItems::getId, craftCheckOrderItems.getId());
                update(craftCheckOrderItems, lambdaUpdate);
            } else {
                craftCheckOrderItems.setCreatedTime(new Timestamp(DateUtil.current()));
                save(craftCheckOrderItems);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<CraftCheckOrderItemsDTO> craftCheckOrderItemsDTOList) {

        List<CraftCheckOrderItems> craftCheckOrderItemsList = BeanUtils.copyProperties(craftCheckOrderItemsDTOList, CraftCheckOrderItems.class);
        return saveOrUpdateBatch(craftCheckOrderItemsList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteCraftCheckOrderItemsLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        CraftCheckOrderItems craftCheckOrderItems = this.getById(id);

        if (ObjectUtil.isNull(craftCheckOrderItems)) {
            log.error("deleteCraftCheckOrderItems() The CraftCheckOrderItems does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteCraftCheckOrderItemsByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			CraftCheckOrderItems craftCheckOrderItems = this.getById(id);
			if (ObjectUtil.isNull(craftCheckOrderItems)) {
				log.error("deleteCraftCheckOrderItems() The CraftCheckOrderItems does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<CraftCheckOrderItems> queryCraftCheckOrderItemsList(CraftCheckOrderItemsFilterDTO craftCheckOrderItemsFilterDTO) {
        CraftCheckOrderItemsQuery craftCheckOrderItemsQuery = BeanUtil.copyProperties(craftCheckOrderItemsFilterDTO, CraftCheckOrderItemsQuery.class);

        return craftCheckOrderItemsMapper.queryCraftCheckOrderItems(craftCheckOrderItemsQuery);
    }

    @Override
    public List<CraftCheckOrderItemsVO> queryCraftCheckOrderItemsVOList(CraftCheckOrderItemsFilterDTO craftCheckOrderItemsFilterDTO) {
        return this.objectConversion(queryCraftCheckOrderItemsList(craftCheckOrderItemsFilterDTO));
    }

    @Override
    public PageVO<CraftCheckOrderItemsVO> queryCraftCheckOrderItems(CraftCheckOrderItemsFilterDTO craftCheckOrderItemsFilterDTO) {
        PageVO<CraftCheckOrderItemsVO> pageVO = new PageVO<>();

        if (craftCheckOrderItemsFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(craftCheckOrderItemsFilterDTO.getCurrentPage(), craftCheckOrderItemsFilterDTO.getPageSize(),
                    craftCheckOrderItemsFilterDTO.getOrderType(), craftCheckOrderItemsFilterDTO.getOrderField());
        }

        List<CraftCheckOrderItems> craftCheckOrderItemsList = queryCraftCheckOrderItemsList(craftCheckOrderItemsFilterDTO);

        PageInfo<CraftCheckOrderItems> pageInfo = new PageInfo<>(craftCheckOrderItemsList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(craftCheckOrderItemsList));

        return pageVO;
    }

    @Override
    public CraftCheckOrderItemsVO queryCraftCheckOrderItemsById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
